Introduction to Python Day 2

Verjinia Metodieva and Daniel Parthier

2025-02-04

Jupyter Notebook

Recap homework

Let’s take a look at the homework

Functions part 2

Goal of today

# TODO: add useful example to introduce class
# Some function we have to decide
def useful_function(par1, par2, par3):
    # some loop
    for i in par1:
        if par1 == "something":
            # do something
        else:
            pass # or something else
    return None # or an object 

Global vs. Local

Short interlude

  • Whole numbers: Integers int
type(1)
int
  • Real numbers: Floats float
type(1.0)
float
  • Most of the time it might not matter1
1 == 1.0
True
  • Sometimes there is a difference and we will see later why

Conditional statements

The important question of what to do “if” something happens.

  • Programming languages are languages
  • if something is True you should do something, else do something else

How to check if everything is true?

For loops

Enumerate

Range

List comprehension

Compare different functions

While loops

  • Perform a task while something is True
  • Be careful:
    • Some loops never finish (get stuck)
    • Make sure that condition for ending the loop can be fullfilled
while check_condition:
    perform_task()

Errors and how to read them

Types of errors

Fix errors